//
//  JYShopCarViewModel.m
//  JYFarm
//
//  Created by 花花 on 2017/10/21.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYShopCartViewModel.h"
#import "JYShopModel.h"
#import "JYShopDepositObjModel.h"
@interface JYShopCartViewModel()


@property (nonatomic, strong) NSMutableArray *depositObjArray;    /** */

@property (nonatomic, strong) JYShopDepositObjModel *shopDepositModel;


@end
@implementation JYShopCartViewModel

- (void)requestShopcartProductList:(NSMutableArray *)arr{

    self.shopcartListArray = arr;
   
    [self.delegate shopcartRequestDataSuccessWithArray:self.shopcartListArray];

}

- (void)selectProductAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected{
    
    JYShopModel *model = [self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    model.selected = isSelected;
    
    NSLog(@"押金价格%zd",[self accountDepositPrice]);
//    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] DepositPrice:[self accountDepositPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
}

- (void)changeCountAtIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count{
    
    JYShopModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    if (count <= 0) {
        count = 1;
    } else if (count > 10000) {
        count = productModel.goodsAmount;
    }
    
    //根据请求结果决定是否改变数据
    productModel.goodsAmount = count;
    
  [self adduserShoppingTrolleyListIndexPath:indexPath count:count];
    
//    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
        [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] DepositPrice:[self accountDepositPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
}

//添加商品到购物车
- (void)adduserShoppingTrolleyListIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count{
    
    
    
    JYShopModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    //cfdd103c-f7ac-495d-b92b-eeb67a1d1e3b
    
    NSDictionary *param = @{
                            @"list":@[@{@"goodsAmount":@(count),@"goodsId":productModel.goodsId,@"goodsName":productModel.goodsName,
                                        @"goodsPictureUrl":productModel.goodsPictureUrl,@"goodsPrice":productModel.goodsPrice,@"originalprice":productModel.originalprice,@"unit":productModel.unit,@"userId":LH.userId,@"categroyId":productModel.categroyId}]
                            };
    
    
    [HTTPRequest POST:kAdduserShoppingTrolleyUrl parameter:param success:^(id resposeObject) {
        
        NSLog(@"添加商品到购物车 %@",resposeObject);
        if (SUCCESS) {
            
    
        }else{
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
            
        }
        
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
        
    }];
    
    
    
    
    
}
//是否全部选择
- (void)selectAllProductWithStatus:(BOOL)isSelected{
    
    
    for (JYShopModel *model in self.shopcartListArray) {
        model.selected = isSelected;
    }
//    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
        [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] DepositPrice:[self accountDepositPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
}

//删除单个
- (void)deleteProductTableView:(UITableView *)tableView AtIndexPath:(NSIndexPath *)indexPath{
    
    JYShopModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    JYWeakSelf;
    params[@"userId"] = LH.userId;
    params[@"goodsId"] = productModel.goodsId;
    params[@"deletShoppingTrolleyType"] = @(0); // 0默认删除单个 1 全删 2批量
    params[@"categroyId"] = productModel.categroyId;
    
    [HTTPRequest POST:kDeleteuserShoppingTrolleyUrl parameter:params success:^(id resposeObject) {
        
        if (SUCCESS) {
            
            [weakSelf.shopcartListArray removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            
        }else{
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
    } failure:^(NSError *error) {
        
        
        [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
    }];
    

//    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
        [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] DepositPrice:[self accountDepositPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
    
    
    if (self.shopcartListArray.count == 0) {
        [self.delegate shopcartClearDeleteAllProducts];
    }
    
}
- (void)beginToDeleteSelectedProducts{
    
    
    NSMutableArray *selectedArr = [NSMutableArray array];
    
    for (JYShopModel *model in self.shopcartListArray) {
        
        if (model.selected) {
            
            [selectedArr addObject:model];
        }
    }
    
    [self.delegate shopcartWillDeleteSelectedProducts:selectedArr];
    
    
}

//将选中的商品进行批量删除
- (void)deleteSelectedProducts:(NSArray *)selectedArray{
    
    NSMutableArray *listArr = [NSMutableArray array];
    for (JYShopModel *model in selectedArray) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"goodsId"] = model.goodsId;
        dic[@"userId"] = LH.userId;
        dic[@"categroyId"] = model.categroyId;
        [listArr addObject:dic];
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"deletShoppingTrolleyType"] = @(2);
    param[@"userId"] = LH.userId;
    [param setValue:listArr forKey:@"list"];
    
      [HTTPRequest POST:kDeleteuserShoppingTrolleyUrl parameter:param success:^(id resposeObject) {
       
          if (SUCCESS) {
              [self.shopcartListArray removeObjectsInArray:selectedArray];
        
                  [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] DepositPrice:[self accountDepositPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
              if (self.shopcartListArray.count == 0) {
                  [self.delegate shopcartClearDeleteAllProducts];
              }

      }else{
              
              [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
          }
      } failure:^(NSError *error) {
          
          [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
      }];
}
//全部删除
- (void)deleteAllProducts{

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"userId"] = LH.userId;
    params[@"deletShoppingTrolleyType"] = @"1";
    
    [HTTPRequest POST:kDeleteuserShoppingTrolleyUrl parameter:params success:^(id resposeObject) {
    
        if (SUCCESS) {
            
            [self.shopcartListArray removeAllObjects];
            
            [self.delegate shopcartClearDeleteAllProducts];
        
    }else{
        
        [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
    }
     } failure:^(NSError *error) {
         
         
         [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
     }];

    
}


//提交订单
- (void)commitOdrerSelectedProducts{
    
    NSMutableArray *commitOrderArr = [NSMutableArray array];
    for (JYShopModel *model in self.shopcartListArray) {
        
        if (model.selected) {
            
            [commitOrderArr addObject:model];
        }
        
    }
    
    if (commitOrderArr.count) {}
    
    [self.delegate shopcartCommitOrderForSelectedProducts:commitOrderArr];
    
    
}

//合计价格
- (double)accountTotalPrice{
    
    double totalPrice = 0.0f;
    
    for (JYShopModel *model in self.shopcartListArray) {
        
        if (model.selected) {
            
            totalPrice += [model.goodsPrice doubleValue]* model.goodsAmount;
            
            
        }
    }
    
    return totalPrice;
    
}
//押金价格
- (double)accountDepositPrice{
    
    double totaldepositPrice = 0.0f;
    
    for (JYShopModel *model in self.shopcartListArray) {
        
        
        if (model.selected) {
           
            if ([model.depositObj isKindOfClass:[NSArray class]]) {
            
//                NSArray *dataArr = model.depositObj;
                
                
                for (NSDictionary *dic in model.depositObj) {
//
                  self.shopDepositModel  = [JYShopDepositObjModel mj_objectWithKeyValues:dic];
//
                }
                  totaldepositPrice += [self.shopDepositModel.goodsPrice doubleValue] *self.shopDepositModel.goodsAmount;

                NSLog(@"totaldepositPrice:%f",totaldepositPrice);
            }else{
            
            }
            
            
        }
    }
    
    return totaldepositPrice;


}




- (NSInteger) accountTotalCount{
    
     NSInteger totalCount = 0;
    
    for (JYShopModel *model in self.shopcartListArray) {
        
        
            totalCount += model.goodsAmount;
        NSLog(@"goodsAmountgoodsAmount%ld",(long)model.goodsAmount);
       
    }
    
    return totalCount;
}


- (BOOL)isAllSelectd{
    
    if (self.shopcartListArray.count == 0)return NO;
    
    BOOL isAllSelected = YES;
    
    for (JYShopModel *model in self.shopcartListArray) {
        if (model.selected == NO) {
            
            isAllSelected = NO;
        }
    }
    return isAllSelected;
    
}

- (NSMutableArray *)depositObjArray{

    if (!_depositObjArray) {
        _depositObjArray = [NSMutableArray array];
    }
    return _depositObjArray;

}

@end
